package com.risetechnology.adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.risetechnology.common.Shared;
import com.risetechnology.krystalkitchen1.R;

@SuppressLint("NewApi")
public class CalendarAdapter extends BaseAdapter {
	static final int FIRST_DAY_OF_WEEK = 0; // Sunday = 0, Monday = 1

	private Context context;

	private java.util.Calendar month;
	private Calendar selectedDate;
	private ArrayList<String> items;

	public CalendarAdapter(Context c, Calendar monthCalendar) {
		month = monthCalendar;
		selectedDate = (Calendar) monthCalendar.clone();
		context = c;
		month.set(Calendar.DAY_OF_MONTH, 1);
		this.items = new ArrayList<String>();
		refreshDays();
	}

	public void setItems(ArrayList<String> items) {
		for (int i = 0; i != items.size(); i++) {
			if (items.get(i).length() == 1) {
				items.set(i, "0" + items.get(i));
			}
		}
		this.items = items;
	}

	public int getCount() {
		return days.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new view for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		TextView dayView;
		TextView titleDayView;
		ImageView iw;

		if (convertView == null) { // if it's not recycled, initialize some
			// attributes
			LayoutInflater vi = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.calendar_item, null);
		}
		dayView = (TextView) v.findViewById(R.id.date);
		dayView.setTypeface(Shared.font);

		titleDayView = (TextView) v.findViewById(R.id.date_type);
		//titleDayView.setTypeface(Shared.font);

		iw = (ImageView) v.findViewById(R.id.date_icon);

		// create date string for comparison
		String date = days[position];

		if (date.length() == 1) {
			date = "0" + date;
		}
		String monthStr = "" + (month.get(Calendar.MONTH) + 1);
		if (monthStr.length() == 1) {
			monthStr = "0" + monthStr;
		}

		// show icon if date is not empty and it exists in the items array
		if (date.length() > 0 && items != null && items.contains(date)) {
			iw.setVisibility(View.VISIBLE);
		} else {
			iw.setVisibility(View.INVISIBLE);
		}


		// disable empty days from the beginning
		if (days[position].equals("")) {
			dayView.setClickable(false);
			dayView.setFocusable(false);
			//v.setBackgroundColor(context.getResources().getColor(
			//	R.color.bg_calendar_item_inactive));
			v.setVisibility(View.GONE);
		} else {
			v.setVisibility(View.VISIBLE);

			// mark current day as focused
			if (month.get(Calendar.YEAR) == selectedDate.get(Calendar.YEAR)
					&& month.get(Calendar.MONTH) == selectedDate
					.get(Calendar.MONTH)
					&& days[position].equals(""
							+ selectedDate.get(Calendar.DAY_OF_MONTH))) {
				//v.setBackgroundResource(R.drawable.item_background_focused);
				v.setBackgroundColor(context.getResources().getColor(
						R.color.bg_tab_selected));

				titleDayView.setVisibility(View.VISIBLE);

				// Show icon if today has event
				boolean hasEvent = Shared.dbAdapter.hasEvent(
						Integer.toString(selectedDate.get(Calendar.DAY_OF_MONTH)), 
						selectedDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US), 
						Integer.toString(selectedDate.get(Calendar.YEAR)));
				if (hasEvent) {
					iw.setImageResource(R.drawable.ic_event_today);
					iw.setVisibility(View.VISIBLE);
				} else {
					iw.setVisibility(View.INVISIBLE);
				}
			} else {
				//v.setBackgroundResource(R.drawable.list_item_background);
				v.setBackgroundColor(context.getResources().getColor(
						R.color.bg_calendar_item_active));

				titleDayView.setVisibility(View.INVISIBLE);
				iw.setImageResource(R.drawable.ic_event);
			}
		}
		dayView.setText(days[position]);
		// Change color of date if the month is not current month
		Calendar currentMonth = Calendar.getInstance();
		if (days[position] != null && days[position].length() > 0) {
			if (month.get(Calendar.YEAR) < currentMonth.get(Calendar.YEAR)
					|| (month.get(Calendar.YEAR) == currentMonth.get(Calendar.YEAR) 
					&&month.get(Calendar.MONTH) < currentMonth.get(Calendar.MONTH))
					||(month.get(Calendar.YEAR) == currentMonth.get(Calendar.YEAR)
					&& month.get(Calendar.MONTH) == currentMonth.get(Calendar.MONTH)
					&& Integer.parseInt(days[position]) < currentMonth.get(Calendar.DAY_OF_MONTH))) {
				dayView.setTextColor(context.getResources().getColor(R.color.bg_tab_normal));
			} else {
				dayView.setTextColor(context.getResources().getColor(R.color.white));
			}
		}

		return v;
	}

	public void refreshDays() {
		// clear items
		items.clear();

		int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);
		int firstDay = (int) month.get(Calendar.DAY_OF_WEEK);

		// figure size of the array
		if (firstDay == 1) {
			days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6)];
		} else {
			days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1)];
		}

		int j = FIRST_DAY_OF_WEEK;

		// populate empty days before first real day
		if (firstDay > 1) {
			for (j = 0; j < firstDay - FIRST_DAY_OF_WEEK; j++) {
				days[j] = "";
			}
		} else {
			for (j = 0; j < FIRST_DAY_OF_WEEK * 6; j++) {
				days[j] = "";
			}
			j = FIRST_DAY_OF_WEEK * 6 + 1; // sunday => 1, monday => 7
		}

		// populate days
		int dayNumber = 1;
		for (int i = j - 1; i < days.length; i++) {
			days[i] = "" + dayNumber;
			dayNumber++;
		}
	}

	// references to our items
	public String[] days;
}